<?php
header("Content-Type: text/html; charset=iso-8859-1");
require('include.php');
if(!check_admin("commission_technique")) redirect('perso.php?action=admin');
else{
	$_SESSION['navigation'] = "salles_admin";
	$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : "nom";
	$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
	$fields = describe('salles');
	$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";	
	$message = "";
	if($action == 'checkno'){
		$q = "select id from salles where no = '$id'";
		$r = ExecRequete($q);
		if(mysql_num_rows($r)) echo 1;
		else echo 0;
	}
	elseif($action == 'list'){
		$table = new table();
		$table->filter = 1;
		$fields['actions'] = "type";
		$hidden = array("id","tel","homologation","lon","lat");
		foreach($fields as $field => $type){
			if(!in_array($field,$hidden)){
				$$field= new table_field($field);
				$$field->print = $field;
				if($type == 'int(1)') $$field->styleclass = 'center';			
				if($field == 'actions')	$actions->styleclass = 'center';
				$table->fields[] = $$field;
			}
			else unset($fields[$field]);
		}
		$q = "select * from salles order by no";
		$r = ExecRequete($q);
		$i = 0;
		while($s = LigneSuivante($r)){
			$row = new table_row();
			foreach($fields as $field => $type){
				if($field != 'id'){
					if($field == 'fichier'){
						$doctype = (ereg(".*\.([a-zA-Z]*)",$s->fichier,$regs)) ? strtolower($regs[1]) : "txt";
						$print = "<a href = 'salles/$s->fichier'><img src = 'images/icon_$doctype.png' alt = '$doctype' width = '14' height = '14' />&nbsp;$s->fichier</a>";
					}
					elseif($type == "int(1)"){
						$checked = ($s->$field) ? "<span class = 'oui'>oui</span>" : "<span class = 'non'>non</span>";
						$print =  "$checked";
					}
					elseif($field == 'actions'){
						$print =  "<img src = 'images/icon_edit.png' alt = 'edit' width = '14' height = '14' style = 'cursor: pointer' onclick = 'edit_salle($s->no)' />";
						$print .=  "<img src = 'images/icon_delete.png' alt = 'del' width = '14' height = '14' style = 'cursor: pointer' onclick = 'if(confirm(\"Voulez-vous vraiment supprimer le fichier ?\"))del_salle($s->no)' />";
					}
					else{
						$print =  $s->$field;
					}
					$row->cols[$field] = $print;
				}
			}
			$table->rows[] = $row;
		}
		echo "<p class = 'center'><button type = 'button' onclick = 'new_salle()' name = 'new'><img src = 'images/icon_new.png' alt = '' width = '14' height = '14' />nouvelle salle</button></p>\n";

		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer();

	}
	elseif($action == 'new'){
		$titre =  "Nouveau\n";
		$content =  "<form action = 'salles_admin.php' method = 'post' name = 'myform' onsubmit = 'return check(this)' >\n";
		foreach($fields as $field => $type){
			if($field == 'lon') $print = 'longitude';
			elseif($field == 'lat') $print = 'latitude';
			else $print = $field;
			$content .= leftCol($print);
			if($field == 'id'){
				$col = 'nouveau';
			}
			elseif($field == 'no'){
				$col = "<input type = 'text' name = '$field' size = '4' onblur = 'verif_nosalle(this.value)' /><span id = 'check_no'></span>\n";
			}
			elseif($type == 'int(1)'){
				$col = "<input type = 'checkbox' checked = 'checked' name = '$field' value = '1' />";
			}
			else{
				$col = "<input type = 'text' name = '$field' size = '20' />\n"; 
			}
			$content .= rightCol($col); 
		}
		$content .= "<br class = 'clear'>\n";
		$content .= "<p class = 'center'><button type = 'submit' name = 'action' value = 'add'>ajouter</button>\n";
		$content .= "<button type = 'button' onclick = 'list_salles()'>annuler</button></p>\n";
		$content .= "</form>\n";
		echo "<div class = 'center' style = 'margin-left: 60px'>\n";
		box($titre,$content,500,1);
		echo "</div>\n";
	}
	elseif($action == 'edit'){
		$salle = new salle($id);
		if($salle->nom){
			$titre =  "Modifier $salle->nom\n";
			$content =  "<form action = 'salles_admin.php' method = 'post' name = 'myform'   >\n";
			foreach($fields as $field => $type){
				if($field == 'lon') $print = 'longitude';
				elseif($field == 'lat') $print = 'latitude';
				else $print = $field;
				$content .= leftCol($print,20);

				if($field == 'id'){
					$col = $salle->id."<input type= 'hidden' name='id' value= '$salle->id' />";
				}
				elseif($type == 'int(1)'){
					$checked = ($salle->$field) ? "checked = 'checked'" : "";
					$col = "<input type = 'checkbox'  name = '$field' value = '1' $checked/>";
				}
				else{
					$col = "<input type = 'text' name = '$field' size = '40' value = '".$salle->$field."' />\n"; 
				}
				$content .= rightCol($col,78); 
			}
			$content .= "<br class = 'clear'>\n";
			$content .= "<p class = 'center'><button type = 'submit' name = 'action' value = 'modif'>Modifier</button>";
			$content .= "\n<button type = 'button' onclick = 'list_salles(\"$order\")'>Retour &agrave; la liste</button>\n<input type = 'hidden' name = 'MAX_FILE_SIZE' value = '9999999' />";
			$content .= "<img src = 'images/icon_map.png' alt = '?' height = '14' width = '14' style = 'cursor: pointer;' onclick = 'show_salle($salle->no)' />";
			$content .= "\n</p>\n";
			$content .= "</form>\n";
			echo "<div class = 'center' style = 'margin-left: 60px'>\n";
			box($titre,$content,500,1);
			echo "</div>\n";
		}		
	}
	elseif($action == 'delete'){
		$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
		if($id){
			$fichier = new salle($id);
			if($fichier->delete()) echo "Le salle a &eacute;t&eacute; effac&eacute;";
			else echo "Désolé, une erreur est survenue. Le salle n'a pas &eacute;t&eacute; effac&eacute;.";			
		} 
		else echo "Désolé, une erreur est survenue. Le salle n'a pas &eacute;t&eacute; effac&eacute;.";
	}

	else{
		if($action == 'modif'){
			$salle = new salle($id);
			foreach($fields as $field => $type){
				$salle->$field = $_REQUEST[$field];
			}
			if($salle->update()) $message = "Modifications enregistr&eacute;es";
			else $message = "Désolé, la salle n'a pas &eacute;t&eacute; modifi&eacute;";
		}
		elseif($action == 'add'){
			$salle = new salle();
			foreach($fields as $field => $type){
				$salle->$field = $_REQUEST[$field];
			}
			if($salle->insert()) 	$message = "salle charg&eacute; avec succ&egrave;s";
			else $message = "D&eacute;sol&eacute;, le salle n'a pas pu &ecirc;tre charg&eacute; !";
		}
		elseif($action == 'link_edit') $salle = new salle($id);
		$script = "function check(myform){\n";
		$script .= "	if(document.myform.no.value == ''){\n";
		$script .= "		alert('Vous n\'avez pas fourni de numéro de salle !!');\n";
		$script .= "		document.myform.no.focus();\n";
		$script .= "		return false;\n";
		$script .= "	}\n";
		$script .= "	else if(document.myform.nom.value == ''){\n";
		$script .= "		alert('Vous n\'avez pas fourni de nom !!');\n";
		$script .= "		document.myform.nom.focus();\n";
		$script .= "		return false;\n";
		$script .= "	}\n";
		$script .= "	else if(document.myform.localite.value == ''){\n";
		$script .= "		alert('Vous n\'avez pas fourni de localité !!');\n";
		$script .= "		document.myform.localite.focus();\n";
		$script .= "		return false;\n";
		$script .= "	}\n";
		$script .= "	return true;\n";
		$script .= "}\n";
		entete("VBCLL salles",$script,'',1);
		if($message) message($message);
		echo "<h1>salles</h1>\n";
		echo "<div id = 'action'></div>\n";
		if($action == 'modif' || $action == 'link_edit') echo "<script type = 'text/javascript'>edit_salle($salle->no)</script>\n";
		else echo "<script type = 'text/javascript'>list_salles()</script>\n";
		pied();
	}	
}


?>
